package goldchampion

import grails.plugins.springsecurity.Secured
@Secured(['ROLE_USER'])
class TimeController {

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	def index = {
		redirect(action: "list", params: params)
	}

	def list = {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		[timeInstanceList: Time.list(params), timeInstanceTotal: Time.count()]
	}

	def create = {
		def timeInstance = new Time()
		timeInstance.properties = params
		return [timeInstance: timeInstance]
	}

	def save = {
		def timeInstance = new Time(params)
		def contador = timeInstance.usuarios.size()
				
		if (timeInstance.tipo_jogo.quantDeJogadores < contador ) {
			flash.message = "${message(code: 'time.erros.quantidade', default: 'Quantidade de usuarios diferente do esperado.' )}"
			render(view: "create", model: [timeInstance: timeInstance])
		}else{
			if (timeInstance.save(flush: true)) {
				flash.message = "${message(code: 'default.created.message', args: [message(code: 'time.label', default: 'Time'), timeInstance.id])}"
				redirect(action: "show", id: timeInstance.id)
			}
			else {
				render(view: "create", model: [timeInstance: timeInstance])
			}
		}
	}
	
	def show = {
		def timeInstance = Time.get(params.id)
		if (!timeInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'time.label', default: 'Time'), params.id])}"
			redirect(action: "list")
		}
		else {
			[timeInstance: timeInstance]
		}
	}

	def edit = {
		def timeInstance = Time.get(params.id)
		if (!timeInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'time.label', default: 'Time'), params.id])}"
			redirect(action: "list")
		}
		else {
			return [timeInstance: timeInstance]
		}
	}

	def update = {
		def timeInstance = Time.get(params.id)
		if (timeInstance) {
			if (params.version) {
				def version = params.version.toLong()
				if (timeInstance.version > version) {

					timeInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [
						message(code: 'time.label', default: 'Time')]
					as Object[], "Another user has updated this Time while you were editing")
					render(view: "edit", model: [timeInstance: timeInstance])
					return
				}
			}

			timeInstance.properties = params

			def contador = timeInstance.usuarios.size()

			if (timeInstance.tipo_jogo.quantDeJogadores < contador ) {
				flash.message = "${message(code: 'time.erros.quantidade', default: 'Quantidade de usuarios diferente do esperado.' )}"
				render(view: "edit", model: [timeInstance: timeInstance])
			}
			else{
				if (!timeInstance.hasErrors() && timeInstance.save(flush: true)) {
					flash.message = "${message(code: 'default.updated.message', args: [message(code: 'time.label', default: 'Time'), timeInstance.id])}"
					redirect(action: "show", id: timeInstance.id)
				}
				else {
					render(view: "edit", model: [timeInstance: timeInstance])
				}
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'time.label', default: 'Time'), params.id])}"
			redirect(action: "list")
		}
	}




	def delete = {
		def timeInstance = Time.get(params.id)
		if (timeInstance) {
			try {
				timeInstance.delete(flush: true)
				flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'time.label', default: 'Time'), params.id])}"
				redirect(action: "list")
			}
			catch (org.springframework.dao.DataIntegrityViolationException e) {
				flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'time.label', default: 'Time'), params.id])}"
				redirect(action: "show", id: params.id)
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'time.label', default: 'Time'), params.id])}"
			redirect(action: "list")
		}
	}
}
